<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\VersionOp;
use App\Http\Controllers\Controller;
use App\Model\AppChannel;
use App\Model\AppVersion;
use App\Util\AliyunUtil;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;

class VersionController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('版本更新配置');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * 编辑
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $channels = AppChannel::getChannels();
        return Admin::grid(AppVersion::class, function (Grid $grid) use ($channels) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('记录ID');
            $grid->app_id('APP包')->display(function ($data) {
                return AppVersion::APPID[$data];
            });
            $grid->platform('平台')->display(function ($data) {
                return AppVersion::PLATFORM[$data];
            });
            $grid->channel('渠道')->display(function ($data) use ($channels) {
                if (!$data) return '全部渠道';
                $channel = explode(',', $data);
                $str = [];
                foreach ($channel as $ch) $str[] = $channels[$ch] ?? '未知渠道';
                return implode('<br/>', $str);
            });
            $grid->update_force('更新类型')->display(function ($data) {
                return $data == 1 ? '强制更新' : '推荐更新';
            });
            $grid->user_profile_id('指定强制更新用户ID')->display(function ($data) {
                return $data == 0 ? '' : $data;
            });
            $grid->update_group('更新角色')->display(function ($data) {
                return AppVersion::UPDATE_GROUP[$data];
            });
            $grid->update_gender('更新性别')->display(function ($data) {
                return AppVersion::UPDATE_GENDER[$data];
            });
            $grid->new_version('更新版本号');
            $grid->update_link('APK包更新链接')->display(function ($data) {
                $link = strpos($data, 'http') === false ? AliyunUtil::IMG_HOST_V2 . $data : $data;
                return "<a href='{$link}' target='_blank'>{$link}</a>";
            });
            $grid->valid_at('更新时间');
            $grid->flag('状态')->display(function ($data) {
                return $data == 1 ? '启用' : '停用';
            });

            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                // $actions->disableEdit();
                $actions->disableView();
                $actions->append(new VersionOp(
                    $actions->getKey(),
                    $actions->row,
                    'app_version/op'
                ));
            });
        });
    }

    /**
     * 新建
     * @return Form
     */
    protected function form()
    {
        $a = request('user_profile_id');
        if(empty($a) && !empty(request('id'))){
            request()->merge('user_profile_id',0);
        }
        return Admin::form(AppVersion::class, function (Form $form) {
            // dump(AppChannel::getChannels());
            $form->select('app_id', 'APP包')->default(0)->options(AppVersion::APPID);
            $form->select('platform', '平台')->default(2)->options(AppVersion::PLATFORM);
            $form->select('update_force', '平台')->default(0)->options([0 => '推荐更新', 1 => '强制更新']);
            $form->text('user_profile_id', '指定用户强制更新外部ID')->default(0);
            $form->select('update_group', '更新角色')->default(0)->options(AppVersion::UPDATE_GROUP);
            $form->select('update_gender', '更新性别')->default(0)->options(AppVersion::UPDATE_GENDER);
            $form->multipleSelect('channel', '更新渠道')->options(AppChannel::getChannels());
            $form->text('new_version', '更新版本号')->default('');
            $form->textarea('desc', '更新文案')->default('');
            $form->text('update_link', 'APK包链接')->default('');
            $form->saving(function (Form $form) {
                !$form->channel && $form->channel = '';
                // $form->channel && $form->channel = implode(',', $form->channel);
            });
            // $form->file('update_link', '上传APK包');
            $form->datetime('valid_at', '更新时间')->default('');
            $form->disableCreatingCheck();
            $form->disableEditingCheck();
            $form->disableViewCheck();
        });
    }

    /**
     * 操作
     */
    function version_op()
    {
        $id = request('id');
        $flag = request('status');
        AppVersion::where('id', $id)->limit(1)->update(['flag' => $flag]);
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
